package cn.tedu.myboot11.controller;

import cn.tedu.myboot11.utils.DBUtils;
import cn.tedu.myboot11.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@Controller
public class UserController {

    @RequestMapping("/reg")
    @ResponseBody
    public String reg(User user) {
        System.out.println("user = " + user);

        //获取连接
        try (
                Connection conn = DBUtils.getConn();
        ) {
            //判断用户名是否存在
            String sql = "select * from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                return "用户已存在!";
            }

            String insertSql = "insert into user values(null,?,?,?)";
            PreparedStatement insertPs = conn.prepareStatement(insertSql);
            insertPs.setString(1, user.getUsername());
            insertPs.setString(2, user.getPassword());
            insertPs.setString(3, user.getNick());
            insertPs.executeUpdate(); //执行
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "注册成功";
    }

    @RequestMapping("/login")
    @ResponseBody
    public String login(User user) {
        try (
                //获取连接
                Connection conn = DBUtils.getConn();
        ) {
            String sql = "select password from user where username=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ResultSet rs = ps.executeQuery();
            if (rs.next()) { //判断是否查询到密码，是则代表用户存在
                String password = rs.getString(1);
                if (password.equals(user.getPassword())) {  //判断密码是否正确
                    return "登录成功";
                }
                return "密码错误！登录失败";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "登录失败";
    }

    @RequestMapping("/showAllUser")
    @ResponseBody
    public String showAllUser() {
        ArrayList<User> list = new ArrayList<>();
        try (
                Connection conn = DBUtils.getConn();
        ) {
            String sql = "select username,password,nick from user";  //查询用户名，密码，昵称
            PreparedStatement ps = conn.prepareStatement(sql);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {  //将数据存入集合
                String username = rs.getString(1);
                String password = rs.getString(2);
                String nick = rs.getString(3);
                User user = new User(username, password, nick);
                list.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //遍历集合把里面的数据装进一个html里面
        String html = "<table border=1>";
        html += "<caption>用户列表</caption>";
        html += "<tr><th>用户名</th><th>密码</th><th>昵称</th></tr>";
        for (User user : list) {
            html += "<tr>";
            html += "<td>" + user.getUsername() + "</td>";
            html += "<td>" + user.getPassword() + "</td>";
            html += "<td>" + user.getNick() + "</td>";
            html += "</tr>";
        }
        html += "</table>";
        return html;
    }
}
